package com.hpin.fansea.service;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.SurroundListAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.SurroundBean;
import com.hpin.fansea.bean.SurroundBussniseList;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 周边商家
 * Created by Aaron on 15/9/11.
 */
public class SurroundActivity extends BaseActivity {

    private LinearLayout ll_surround_food; // 活动按钮区
    private LinearLayout ll_surround_transaction;
    private LinearLayout ll_surround_movie;
    private LinearLayout ll_surround_leisure;
    private LinearLayout ll_surround_attractions;
    private LinearLayout ll_surround_recommend;

    private ListView list_surround_food;
    private ListView list_surround_transaction;
    private ListView list_surround_movie;
    private ListView list_surround_leisure;
    private ListView list_surround_attractions;
    private ListView list_surround_recommend;

//    private Handler mHandler;
    private SurroundListAdapter foodListAdapter = null;
    private SurroundListAdapter transactionListAdapter = null;
    private SurroundListAdapter movieListAdapter = null;
    private SurroundListAdapter leisureListAdapter = null;
    private SurroundListAdapter attractionsListAdapter = null;
    private SurroundListAdapter recommendListAdapter = null;
//
    private List<SurroundBussniseList> surroundBussniseLists = null;
    // 更多按钮
    private LinearLayout surround_food_more;
    private LinearLayout surround_attractions_more_;
    private LinearLayout surround_movie_more;
    private LinearLayout surround_leisure_more;
    private LinearLayout surround_transaction_more;
    private LinearLayout surround_recommend_more;

    private Intent mIntent;

    private TextView food_more;

    private TextView attractions_more;

    private TextView movie_more;

    private TextView leisure_more;

    private TextView transaction_more;

    private TextView recommend_more;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_service_surround);

        initView();

        getSurroundData();

    }

    /**
     * 获取周边商家数据
     */
    private void getSurroundData() {

        HttpUtil.getInstance().setGetRequest("获取中...", this,
                Constant.WEB_ROOT + Constant.BUSSNISEINDEXLIST + "?token=" + sp.getString(Constant.token, "") + "&communityId=" + sp.getString(Constant.communityId, "")
                        + "&systemType=" + Constant.SYSTEM_TYPE, new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            SurroundBean surroundBean = JSONObject.parseObject(str, SurroundBean.class);

                            if (Constant.STATUS_SUCESS.equals(surroundBean.status)) {

                                //今日推荐
                                surroundBussniseLists = surroundBean.resulta;
                                // 共多少家
                                recommend_more.setText("共" + surroundBean.totalCounta + "家");

                                if (surroundBussniseLists != null && recommendListAdapter != null) {
                                    recommendListAdapter.setData(surroundBussniseLists);
                                    recommendListAdapter.notifyDataSetChanged();
                                }

                                surroundBussniseLists = surroundBean.result4;
                                // 共多少家
                                transaction_more.setText("共" + surroundBean.totalCount4 + "家");

                                if (surroundBussniseLists != null && transactionListAdapter != null) {
                                    transactionListAdapter.setData(surroundBussniseLists);
                                    transactionListAdapter.notifyDataSetChanged();
                                }

                                surroundBussniseLists = surroundBean.result1;
                                // 共多少家
                                food_more.setText("共" + surroundBean.totalCount1 + "家");

                                if (surroundBussniseLists != null && foodListAdapter != null) {
                                    foodListAdapter.setData(surroundBussniseLists);
                                    foodListAdapter.notifyDataSetChanged();
                                }

                                surroundBussniseLists = surroundBean.result3;
                                // 共多少家
                                movie_more.setText("共" + surroundBean.totalCount3 + "家");
                                if (surroundBussniseLists != null && movieListAdapter != null) {
                                    movieListAdapter.setData(surroundBussniseLists);
                                    movieListAdapter.notifyDataSetChanged();
                                }

                                surroundBussniseLists = surroundBean.result2;
                                // 共多少家
                                leisure_more.setText("共" + surroundBean.totalCount2 + "家");
                                if (surroundBussniseLists != null && leisureListAdapter != null) {
                                    leisureListAdapter.setData(surroundBussniseLists);
                                    leisureListAdapter.notifyDataSetChanged();
                                }

                                surroundBussniseLists = surroundBean.result5;
                                // 共多少家
                                attractions_more.setText("共" + surroundBean.totalCount5 + "家");
                                if (surroundBussniseLists != null && attractionsListAdapter != null) {
                                    attractionsListAdapter.setData(surroundBussniseLists);
                                    attractionsListAdapter.notifyDataSetChanged();
                                }

                            }else {
                                showToast(surroundBean.message);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast("获取周边商家信息失败");
                        }

                    }
                }, null);

    }

    private void initView() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.surround));

        surroundBussniseLists = new ArrayList<SurroundBussniseList>();

        // 首页的listview
        list_surround_food = (ListView) findViewById(R.id.surround_food_list);
        foodListAdapter = new SurroundListAdapter(this, surroundBussniseLists);
        list_surround_food.setAdapter(foodListAdapter);

        list_surround_transaction = (ListView) findViewById(R.id.surround_hotel_list);
        transactionListAdapter = new SurroundListAdapter(this, surroundBussniseLists);
        list_surround_transaction.setAdapter(transactionListAdapter);

        list_surround_movie = (ListView) findViewById(R.id.surround_movie_list);
        movieListAdapter = new SurroundListAdapter(this, surroundBussniseLists);
        list_surround_movie.setAdapter(movieListAdapter);

        list_surround_leisure = (ListView) findViewById(R.id.surround_ktv_list);
        leisureListAdapter = new SurroundListAdapter(this, surroundBussniseLists);
        list_surround_leisure.setAdapter(leisureListAdapter);

        list_surround_attractions = (ListView) findViewById(R.id.surround_attractions_list);
        attractionsListAdapter = new SurroundListAdapter(this, surroundBussniseLists);
        list_surround_attractions.setAdapter(attractionsListAdapter);

        list_surround_recommend = (ListView) findViewById(R.id.surround_recommend_list);
        recommendListAdapter = new SurroundListAdapter(this, surroundBussniseLists);
        list_surround_recommend.setAdapter(recommendListAdapter);

        //各个分类的布局
        ll_surround_food = (LinearLayout) findViewById(R.id.ll_surround_food);
        ll_surround_transaction = (LinearLayout) findViewById(R.id.ll_surround_transaction);
        ll_surround_movie = (LinearLayout) findViewById(R.id.ll_surround_movie);
        ll_surround_leisure = (LinearLayout) findViewById(R.id.ll_surround_leisure);
        ll_surround_attractions = (LinearLayout) findViewById(R.id.ll_surround_attractions);
        ll_surround_recommend = (LinearLayout) findViewById(R.id.ll_surround_recommend);
        // 各个标题
        surround_food_more = (LinearLayout) findViewById(R.id.surround_food_more);
        surround_attractions_more_ = (LinearLayout) findViewById(R.id.surround_attractions_more);
        surround_movie_more = (LinearLayout) findViewById(R.id.surround_movie_more);
        surround_leisure_more = (LinearLayout) findViewById(R.id.surround_leisure_more);
        surround_transaction_more = (LinearLayout) findViewById(R.id.surround_transaction_more);
        surround_recommend_more = (LinearLayout) findViewById(R.id.surround_recommend_more);
        // 共多少家
        food_more = (TextView) findViewById(R.id.food_more);
        attractions_more = (TextView) findViewById(R.id.attractions_more);
        movie_more = (TextView) findViewById(R.id.movie_more);
        leisure_more = (TextView) findViewById(R.id.leisure_more);
        transaction_more = (TextView) findViewById(R.id.transaction_more);
        recommend_more = (TextView) findViewById(R.id.recommend_more);

        ll_surround_food.setOnClickListener(this);
        surround_attractions_more_.setOnClickListener(this);
        surround_movie_more.setOnClickListener(this);
        ll_surround_leisure.setOnClickListener(this);
        ll_surround_attractions.setOnClickListener(this);
        ll_surround_recommend.setOnClickListener(this);

        surround_food_more.setOnClickListener(this);
        ll_surround_transaction.setOnClickListener(this);
        ll_surround_movie.setOnClickListener(this);
        surround_leisure_more.setOnClickListener(this);
        surround_transaction_more.setOnClickListener(this);
        surround_recommend_more.setOnClickListener(this);

        iv_left.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.ll_surround_food:
            case R.id.surround_food_more:

                mIntent = new Intent(this, SurroundListActivity.class);

                mIntent.putExtra("type", Constant.FOOD);
                mIntent.putExtra("title", getResources().getString(R.string.surround_food));

                startActivity(mIntent);

                break;
            case R.id.ll_surround_transaction:
            case R.id.surround_transaction_more:

                mIntent = new Intent(this, SurroundListActivity.class);

                mIntent.putExtra("type", Constant.TRANSATION);
                mIntent.putExtra("title", getResources().getString(R.string.surround_transaction));

                startActivity(mIntent);

                break;
            case R.id.ll_surround_movie:
            case R.id.surround_movie_more:

                mIntent = new Intent(this, SurroundListActivity.class);

                mIntent.putExtra("type", Constant.MOVIE);
                mIntent.putExtra("title", getResources().getString(R.string.surround_movie));

                startActivity(mIntent);

                break;
            case R.id.ll_surround_leisure:
            case R.id.surround_leisure_more:

                mIntent = new Intent(this, SurroundListActivity.class);

                mIntent.putExtra("type", Constant.LEISURE);
                mIntent.putExtra("title", getResources().getString(R.string.surround_leisure));

                startActivity(mIntent);

                break;
            case R.id.ll_surround_attractions:
            case R.id.surround_attractions_more:

                mIntent = new Intent(this, SurroundListActivity.class);

                mIntent.putExtra("type", Constant.ATTRACTION);
                mIntent.putExtra("title", getResources().getString(R.string.surround_attractions));


                startActivity(mIntent);

                break;
            case R.id.ll_surround_recommend:
            case R.id.surround_recommend_more:

                mIntent = new Intent(this, SurroundListActivity.class);

                mIntent.putExtra("type", Constant.RECOMMEND);
                mIntent.putExtra("title", getResources().getString(R.string.surround_recommend));

                startActivity(mIntent);

                break;

            default:
                break;
        }
    }
}